<template>
  <div class="mod-user">
    <el-form :inline="true" :model="formData" label-width="100px" label-position="right" >
      <el-form-item label="合伙人名称">
        <el-input @change="searchChange" placeholder="合伙人名称" v-model="formData.name" size="small" clearable></el-input>
      </el-form-item>
      <el-form-item label="合伙人手机">
        <el-input @change="searchChange" placeholder="手机号" v-model="formData.mobile" size="small" clearable></el-input>
      </el-form-item>
      <el-form-item label="合伙人角色">
        <el-select size="small" v-model="formData.partnerRole" @change="searchChange" clearable>
          <el-option v-for="(item, index) in partnerRoleList" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="加盟日期">
        <el-date-picker
          v-model="formData.createDate"
          @change="searchChange"
          size="small"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所在城市">
        <el-cascader
          @change="searchChange"
          v-model="formData.regionIds"
          :options="options"
          :props="props"
          size="small" clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="addOrUpdate()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click.stop="searchChange">搜索</el-button>
      </el-form-item>
    </el-form>
    <div class="sumary-list" v-if="false">
      <div>合伙人数量：{{page.total}}</div>
      <div>加盟费：{{page.total}}</div>
      <div>当前可提现金额：{{page.total}}</div>
      <div>未入账金额：{{page.total}}</div>
      <div>提现审核中金额：{{page.total}}</div>
    </div>
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :option="tableOption"
               @size-change="sizeChange"
               @current-change="currentChange">
      <template slot-scope="scope" slot="parnterInfo">
        <div class="cell">
          <div>姓名：<span>{{scope.row.realName}}</span></div>
          <div>手机号：<span>{{scope.row.mobile}}</span></div>
          <div>所在城市：<span>{{scope.row.regionNames}}</span></div>
          <div>角色：<span>{{getRoleTxt(scope.row.partnerRole)}}</span></div>
          <div>运营商：<span>{{scope.row.merchantName}}</span></div>
        </div>
      </template>
      <template slot-scope="scope" slot="addPartnerInfo">
        <div>
          <div>加盟费：<span>{{scope.row.joinMoney}}</span></div>
          <div>加盟时间：<span>{{scope.row.joinDate}}</span></div>
          <div>授权充电桩台数：<span>{{scope.row.creditNum}}</span></div>
          <div>电动车片区分佣比例：<span>{{scope.row.defaultDivideRate}}</span></div>
          <div>汽车片区分佣比例：<span>{{scope.row.carDivideRate}}</span></div>
        </div>
      </template>
      <template slot-scope="scope" slot="rate">
        <div>
          <div>昨日使用率：<span>{{scope.row.yesterdayUseRate}}</span></div>
          <div>周使用率：<span>{{scope.row.lastWeekUseRate}}</span></div>
          <div>本月使用率：<span>{{scope.row.lastMonthUseRate}}</span></div>
        </div>
      </template>
      <template slot-scope="scope" slot="pileInfo">
        <div>授权充电桩数：<span>{{scope.row.creditNum}}</span></div>
        <div>已安装充电桩台数：<span>{{scope.row.installChargingCount}}</span></div>
        <div>未安装充电桩台数：<span>{{scope.row.noInstallChargingCount}}</span></div>
      </template>
      <template slot-scope="scope" slot="managerInfo">
        <div>总分佣金额：<span>{{scope.row.totalDivideMoney}}</span></div>
        <div>今日分佣金额：<span>{{scope.row.todayDivideMoney}}</span></div>
        <div>当前可提现金额：<span>{{scope.row.amount}}</span></div>
      </template>
      <template slot-scope="scope"
                slot="status">
        <el-tag v-if="scope.row.status === 0"
                size="small"
                type="danger">禁用</el-tag>
        <el-tag v-else
                size="small">正常</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="primary"
                   icon="el-icon-edit"
                   size="small"
                   @click.stop="addOrUpdate(scope.row.id)">编辑</el-button>

        <el-button type="primary"
                   size="small"
                   @click.stop="lookDetail(scope.row.id)">查看</el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script>
  import { tableOption } from '@/crud/partner/partnetList'
  export default {
    data () {
      return {
        dataList: [],
        dataListLoading: false,
        tableOption: tableOption,
        formData: {
          name: '',
          mobile: '',
          partnerRole: '',
          createDate: '',
          regionIds: []
        },
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        options: [],
        props: {
          multiple: false,
          label: 'name',
          value: 'id',
          children: 'childs'
        },
        partnerRoleList: [
          {
            label: '公司合伙人',
            value: 0
          },
          {
            label: '市场合伙人',
            value: 1
          },
          {
            label: '管理合伙人',
            value: 2
          },
          {
            label: '分佣合伙人',
            value: 3
          },
          {
            label: '购买合伙人',
            value: 4
          }
        ],
        totalCount: 0,
        todayCount: 0,
        remainTotalMoney: 0,
        remainMoney: 0,
        remainGiveMoney: 0
      }
    },

    mounted () {
      this.getAreaList()
      this.getDataList()
    },

    methods: {
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/merchant/partner/list'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              params
            )
          )
        }).then(({ data }) => {
          this.dataList = data.data.records
          this.page.total = data.data.total
          this.dataListLoading = false
        })
      },

      getRoleTxt (role) {
        if (role == 0) {
          return '公司合伙人'
        }
        else if (role == 1){
          return '市场合伙人'
        }
        else if (role == 2){
          return '管理合伙人'
        }
        else if (role == 3) {
          return '分佣合伙人'
        }
        else if (role == 4) {
          return '购买合伙人'
        }
      },

      getAreaList () {
        this.$http({
          url: this.$http.adornUrl('/v1/merchant/system/get/all/region'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({ data }) => {
          this.options = data.data
        })
      },

      // 条件查询
      searchChange () {
        console.log(this.formData)
        if(this.formData.regionIds.length >= 3){
          this.formData.regionId = this.formData.regionIds[2]
        }
        else {
          this.formData.regionId = ''
        }
        if(this.formData.createDate&&this.formData.createDate.length > 0) {
          this.formData.joinStartTime = this.formData.createDate[0]
          this.formData.joinEndTime = this.formData.createDate[1]
        }
        else {
          this.formData.joinStartTime = ''
          this.formData.joinEndTime = ''
        }
        this.page.currentPage=1
        this.getDataList(this.page, this.formData)
      },

      addOrUpdate (id) {
        console.log(id)
        this.$router.push({
          name: 'addPartner',
          query: {
            id: id,
            type: 2
          }
        })
      },

      lookDetail (id) {
        this.$router.push({
          name: 'addPartner',
          query: {
            id: id,
            type: 1
          }
        })
      },
      sizeChange(val){
        this.page.pageSize=val
        this.getDataList(this.page, this.formData)
      },
      currentChange(val){
        this.page.currentPage=val
        this.getDataList(this.page, this.formData)
      }
    }
  }
</script>

<style lang="scss">
  /*.cell{*/
    /*text-align: left;*/
  /*}*/
  .el-input{
    width: 200px;
  }
</style>
